﻿#region Header

/*
    This file is part of NDoctor.

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/

#endregion Header

namespace Probel.AspTest.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Probel.AspTest.Domain;
    using Probel.AspTest.Domain.Components;
    using System.Web.UI.DataVisualization.Charting;
    using System.Drawing;
    using System.IO;

    public class ChartController : Controller
    {
        private readonly IChartComponent Component = ComponentFactory.New<IChartComponent>();
        #region Methods

        //
        // GET: /Chart/
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /AvgUsage/
        public ActionResult AvgUsage()
        {
            return View();
        }

        public ActionResult ExecutionTime()
        {
            return View();
        }

        /// <summary>
        /// http://stackoverflow.com/questions/8474822/displaying-database-image-bytes-in-razor-mvc3
        /// http://stackoverflow.com/questions/6281520/charting-in-asp-net-mvc-3
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAverageUsage()
        {
            var data = this.Component.GetUsageByComponent();


            var chart = new Chart()
            {
                Height = 768,
                Width = 1024,
                BackHatchStyle = ChartHatchStyle.None
            };

            var area = new ChartArea();
            area.AxisX.MajorGrid.Enabled
                //= area.AxisY.MajorGrid.Enabled
                = false;
            area.AxisX.Interval = 1;
            // configure your chart area (dimensions, etc) here.
            chart.ChartAreas.Add(area);

            // create and customize your data series.
            var series = new Series();
            foreach (var item in data.Points)
            {
                series.Points.AddXY(item.X, item.Y);
            }
            series.Label = "#PERCENT{P0} (#VAL{0})";
            series.Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
            series.Color = ColorTranslator.FromHtml("#5c87b2");
            series.ChartType = SeriesChartType.Bar;
            series["PieLabelStyle"] = "Outside";

            chart.Series.Add(series);

            var returnStream = new MemoryStream();
            chart.ImageType = ChartImageType.Png;
            chart.SaveImage(returnStream);
            returnStream.Position = 0;
            return new FileStreamResult(returnStream, "image/png");
        }

        public ActionResult GetExecutionTimeGraph()
        {
            var data = this.Component.ExecutionTimeGraph();


            var chart = new Chart()
            {
                Height = 768,
                Width = 1024,
                BackHatchStyle = ChartHatchStyle.None
            };

            var area = new ChartArea();
            area.AxisX.MajorGrid.Enabled
                = area.AxisY.MajorGrid.Enabled
                = false;
            // configure your chart area (dimensions, etc) here.
            chart.ChartAreas.Add(area);

            // create and customize your data series.
            var series = new Series();
            foreach (var item in data.Points)
            {
                series.Points.AddXY(item.X, item.Y);
            }
            series.Label = "#VAL{0} ms";
            series.Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
            series.Color = ColorTranslator.FromHtml("#5c87b2");
            series.ChartType = SeriesChartType.Line;
            series["PieLabelStyle"] = "Outside";

            chart.Series.Add(series);

            var returnStream = new MemoryStream();
            chart.ImageType = ChartImageType.Png;
            chart.SaveImage(returnStream);
            returnStream.Position = 0;
            return new FileStreamResult(returnStream, "image/png");
        }

        #endregion Methods
    }
}